'use strict';

angular.module('afasApp')
  .controller('CurrenciesCtrl', ['$scope', 'Services', '$state', function ($scope, Services, $state) {

    $scope.currencies = [];
    $scope.currencyNew = {};
    $scope.errors = {};

    // $scope.currencies = Currencies.getCurrencies();

    function applyRemoteData( newData ) {
      $scope.currencies = newData;
    }

    function loadRemoteData() {
      Services.getItems('currencies')
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addCurrency = function () {
      $scope.errors = {};
      Services.add('currencies', $scope.currencyNew)
      .then(function(){
        $scope.currencyNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteCurrency = function (currency) {
      Services.delete('currencies', currency)
      .then(function(){
        loadRemoteData();
      });
    };
  }]).controller('CurrencyCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.currentCurrency = {};
    $scope.currentID = $stateParams.id;
    // $scope.currentCurrency =Currencies.getCurrency($stateParams.id);
    Services.getItem('currencies', $scope.currentID)
    .then(function(results){
      $scope.currentCurrency = results.data;
    });

    $scope.updateCurrency = function () {
      Services.update('currencies', $scope.currentCurrency, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };

  }]);